/**
 * 战斗的脚本
 */
var diaoxueAct;

function createFight(){
	//待机
	var f = new actionBase();
	createAnima(f.animimgs,"/RPG/sucai/char01/361-",".png",8,true);
	f.Statehold =true;
	f.stepcnt = 8;
	f.autoreplay =true;
	f.x=FIGHT_X1;
	f.y=FIGHT_Y1;
	f.intval =4;
	f.randerlayer = LAYER_BG;
	AcceptSprite.attackActions[0]=f;
	//攻击
	var f3 = new actionBase();
	createAnima(f3.animimgs,"/RPG/sucai/char01/782-",".png",9,true);
	f3.stepcnt =9;
	f3.intval =2;
	f3.x=FIGHT_X1-300;
	f3.y=FIGHT_Y1-90;
	f3.randerlayer = LAYER_BG;
	f3.actEx=ActEX;
	AcceptSprite.attackActions[1]=f3;
	
	//bei攻击
	var f6 = new actionBase();
	createAnima(f6.animimgs,"/RPG/sucai/char01/780-",".png",3,true);
	f6.stepcnt =3;
	f6.intval =2;
	f6.x=FIGHT_X1;
	f6.y=FIGHT_Y1;
	f6.randerlayer = LAYER_BG;
	AcceptSprite.attackActions[2]=f6;
	
	yujing  = new sprite("fish");
	sprites.push(yujing);
	var f2 = new actionBase();
	createAnima(f2.animimgs,"/RPG/sucai/char02/1218-",".png",3,true);
	f2.Statehold =true;
	f2.stepcnt = 3;
	f2.autoreplay =true;
	f2.x=FIGHT_X2;
	f2.y=FIGHT_Y2;
	f2.intval =8;
	f2.randerlayer = LAYER_BG;
	yujing.attackActions[0]=f2;
	
	var f4 = new actionBase();
	createAnima(f4.animimgs,"/RPG/sucai/char02/1221-",".png",2,true);
	f4.stepcnt = 2;
	f4.x=FIGHT_X2-120;
	f4.y=FIGHT_Y2-70;
	f4.intval =3;
	f4.randerlayer = LAYER_BG;
	yujing.attackActions[2]=f4;
	
	var f5 = new actionBase();
	createAnima(f5.animimgs,"/RPG/sucai/char02/1219-",".png",6,true);
	f5.stepcnt = 6;
	f5.x=FIGHT_X2;
	f5.y=FIGHT_Y2-70;
	f5.intval =3;
	f5.randerlayer = LAYER_BG;
	f5.actEx=ActEX2;
	yujing.attackActions[1]=f5;
	
	var f7 = new Disappear();
	f7.animimgs[0] = new Image();
	f7.animimgs[0].src = "/RPG/sucai/char02/1218-1.png";
	f7.x=FIGHT_X2;
	f7.y=FIGHT_Y2;
	f7.width = 340;
	f7.height = 276;
	f7.intval =2;
	
	yujing.attackActions[3]=f7;
	
	
	var f8 = new actionBase();
	createAnima(f8.animimgs,"/RPG/sucai/char01/790-",".png",23,true);
	f8.stepcnt = 23;
	f8.Statehold =true;
	f8.x=FIGHT_X1-100;
	f8.y=FIGHT_Y1-70;
	f8.intval =2;
	f8.randerlayer = LAYER_BG;
	AcceptSprite.attackActions[4]=f8;
	
	
	AcceptSprite.actions.push(f);
	yujing.actions.push(f2);
	
	createYujing();
	
	$("#fightbar").show();
	$("#HPbar").show();
	$("#Qbar").show();
	
	$("#HPval").show();
	$("#Qval").show();
	
	updateFState();
	
	
	
}
//玩家攻击
ActEX = function (){
	if (this.step>=this.stepcnt){
		//攻击结束
		//AI
		doAI();
	} else if (this.step == 0 && this.sst==1){
		yujing.attactted();
		diaoxueAct = new diaoxue();
		diaoxueAct.x =150;diaoxueAct.y =150;
		diaoxueAct.number =FightEffect(AcceptSprite,yujing);;
		diaoxueAct.init();
		Addaction(null,diaoxueAct);

	}
}
//鱼精攻击
ActEX2 = function (){
	if (this.step>=this.stepcnt){
		showFMenu();
	} else if (this.step == 3 && this.sst==1){
		AcceptSprite.attactted();
		
		diaoxueAct.x =450;diaoxueAct.y =150;
		diaoxueAct.number =FightEffect(yujing,AcceptSprite);
		diaoxueAct.init();
		updateFState();
		if (AcceptSprite.properties.get("hp")<=0){
			//todo
		}
	} else if (this.step == 1){
		this.x=FIGHT_X2 + 70;
	}
}


function doAI(){
		if (yujing.properties.get("hp")<=0){
			yujing.over();
			AcceptSprite.vic();
			showFDlg("<center>胜利！<br>演示到此为止。</center>");
		}else 
			yujing.attact();
}


//掉血-数字
var diaoxue = function (){
	this.stepcnt=10;//not fix
	this.Statehold = true; 
	this.number = 0;
	this.numImg =  new Array();
	//this.intval =2;
	this.initEx= function(){
		this.number = this.number + "";
		this.stepcnt = this.number.length*3+12;
		createAnima(this.numImg,"/RPG/sucai/ui/80-",".png",10,false);
		this.step =0;
	}
	this.actEx = function (){
		if (this.step >=this.stepcnt-1 ) return;

		var f = Math.floor(this.step/3);//第几个浮动的字
		var s = this.step - f*3;
		
		for (var i=0;i<this.number.length;i++){
			RanderJobLst_Beyound.push(new RanderJob());
			RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].x = this.x + i*8;
			RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].y = this.y;
			RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].imgobj 
				= this.numImg[parseInt(this.number[i])];
			
			if (f ==i){
				switch (s){ //这里可以换图片以达到更好效果 从白到黄到红
				case 0:
					RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].y = this.y-5;
					break;
				case 1:
					RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].y = this.y+2;
				case 2:	
				}
			}  
			var k = this.step - this.number.length*3 ;
			if (k>0) RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].y = this.y-k*(8+k/4);
		}
		
	}
	
}
diaoxue.prototype =new actionBase();


function FightEffect(chuzhao,beida){
	//TODO 以后应该在PHP端返回
	var shanghai = SpriteProSystem("gongji",chuzhao);
	shanghai = Math.round(shanghai-SpriteProSystem("fangshou",beida)/2);
	var xue = beida.properties.get("hp");
	xue = xue-shanghai;
	
	beida.properties.put("hp",xue);
	return shanghai;
}

//TODO 暂时
function createYujing(){
	yujing.properties.put("hp",200);
	yujing.properties.put("q",160);
	yujing.properties.put("exp",2000);
	
	yujing.properties.put("jin",80);
	yujing.properties.put("mu",20);
	yujing.properties.put("shui",100);
	yujing.properties.put("huo",20);
	yujing.properties.put("tu",20);
}

